package com.boc.langxiang.presenter.common;


import com.boc.langxiang.contract.common.AddressManageContract;
import com.boc.langxiang.entity.request.DeleteAddressRequest;
import com.boc.langxiang.entity.request.SetDefaultAddressRequest;
import com.boc.langxiang.entity.request.TokenRequest;
import com.boc.langxiang.entity.response.AddressListContent;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CreateTime: 2016-06-14上午10:40
 * Author: wjkjinke00@126.com
 * Description:
 */
public class AddressManagePresenter implements AddressManageContract.Presenter {

    private final ApiService mService;
    private AddressManageContract.View mView;

    public AddressManagePresenter(AddressManageContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        mView = view;
    }

    @Override
    public void getAddressList() {
        mView.showLoading();
        TokenRequest request = new TokenRequest();
        Observable<BaseResponse<AddressListContent>> register = mService.getAddressList(request.token);
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<AddressListContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<AddressListContent> response) {
                        mView.onGetAddressSuccess(response);
                    }
                });
    }

    @Override
    public void deleteAddress(int addrId) {
        mView.showDeleteLoading();
        DeleteAddressRequest request = new DeleteAddressRequest();
        request.addrId = addrId;
        Observable<BaseResponse> register = mService.deleteAddress(request.params());
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.onDeleteAddressSuccess();
                    }
                });
    }

    @Override
    public void setDefaultAddress(int addrId) {
        mView.showSetDefaultLoading();
        SetDefaultAddressRequest request = new SetDefaultAddressRequest();
        request.addrId = addrId;
        Observable<BaseResponse> register = mService.setDefaultAddress(request.params());
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.setDefaultAddressSuccess();
                    }
                });
    }
}
